#!/bin/sh
# This script builds and runs INET with the various features turned on and off testing
# whether features can be enabled and disabled independently without causing
# build or runtime failures.

set -e

FEATURETOOL=opp_featuretool

# Set the SKIPPED_FEATURES environment variable to empty string to allow enabling
# the features that require NSC or libav*.
if [ -z ${SKIPPED_FEATURES+x} ]; then
  skipped_features="VoipStream"
else
  skipped_features=${SKIPPED_FEATURES}
fi

# These two variables control which feature subset is being checked.
# This makes it easy to split a single test across multiple machines.
# If for example TOTAL is 3, and INDEX is 1, only every 3*i+1-th
# feature is checked. Their value is taken over from the environment.
# The default is to include all features. The "default", "all_enabled"
# and "all_disabled" combinations are always done in INDEX 0.
SPLIT_INDEX=${SPLIT_INDEX:-"0"}
SPLIT_TOTAL=${SPLIT_TOTAL:-"1"}

#skip the first n tests
SKIP_TESTS=${SKIP_TESTS:-"0"}

exitcode=0

testindex=0


# test procedure that is run for each test case (assumes to be in INET_ROOT)
runtest() {
  RED='\033[0;31m'
  GREEN='\033[0;32m'
  NC='\033[0m' # No Color
  (make makefiles >/dev/null && make cleanall >/dev/null && make makefiles >/dev/null) || { echo "*** $testindex. $1 prepare build: ${RED}FAIL${NC}" ; return 1; }
  make -j8 >$LOG_DIR/$1-build.out 2>$LOG_DIR/$1-build.err || { echo "*** $testindex. $1 building: ${RED}FAIL${NC}" ; return 1; }
  (cd $DIR; opp_run -l ../../src/INET -u Cmdenv -n ../../src:. >$LOG_DIR/$1-run.out 2>$LOG_DIR/$1-run.err) || { echo "*** $testindex. $1 running: ${RED}FAIL${NC}" ; return 1; }
  echo "*** $testindex. $1: ${GREEN}PASS${NC}"; return 0;
}

cd $(dirname $0)
DIR=$(pwd)
LOG_DIR=$(pwd)/_log
rm -rf $LOG_DIR; mkdir -p $LOG_DIR
cd ../..

echo "Running feature build and run tests..."
base_features=$($FEATURETOOL list | grep -v _examples | awk '{print $2;}' | awk "NR % ${SPLIT_TOTAL} == ${SPLIT_INDEX}")
if [ -n "$skipped_features" ]; then
  echo "Skipping the following features: $skipped_features"
fi

if [ "$SPLIT_TOTAL" != "1" ]; then
  echo "Testing feature subset ${SPLIT_INDEX} out of ${SPLIT_TOTAL}:" $base_features
fi

if [ "$SPLIT_INDEX" = "0" ]; then

  # test with all features disabled
  if [ "$testindex" -lt "$SKIP_TESTS" ] ; then
    echo "*** $testindex. all_disabled: SKIPPED"
  else
    $FEATURETOOL -q disable -f all
    runtest all_disabled || exitcode=1
  fi
  testindex=$((testindex+1))

  # test with default features enabled (except skipped ones)
  if [ "$testindex" -lt "$SKIP_TESTS" ] ; then
    echo "*** $testindex. default: SKIPPED"
  else
    $FEATURETOOL -q reset
    if [ -n "$skipped_features" ]; then
      $FEATURETOOL -q disable -f $skipped_features
    fi
    runtest default || exitcode=1
  fi
  testindex=$((testindex+1))

  # test with all features enabled (except skipped ones)
  if [ "$testindex" -lt "$SKIP_TESTS" ] ; then
    echo "*** $testindex. all_enabled: SKIPPED"
  else
    $FEATURETOOL -q enable -f all
    if [ -n "$skipped_features" ]; then
      $FEATURETOOL -q disable -f $skipped_features
    fi
    runtest all_enabled || exitcode=1
  fi
  testindex=$((testindex+1))
fi

# test with each feature enabled - one by one
for feature in $base_features; do
  if echo $skipped_features | grep -q $feature || [ "$testindex" -lt "$SKIP_TESTS" ] ; then
    echo "*** $testindex. ${feature}_enabled: SKIPPED"
  else
    $FEATURETOOL -q disable -f all
    $FEATURETOOL -q enable -f $feature
    runtest ${feature}_enabled || exitcode=1
  fi
  testindex=$((testindex+1))
done

# test with each feature disabled - one by one
for feature in $base_features; do
  $FEATURETOOL -q enable -f all
  if [ -n "$skipped_features" ]; then
    $FEATURETOOL -q disable -f $skipped_features
  fi
  if echo $skipped_features | grep -q $feature  || [ "$testindex" -lt "$SKIP_TESTS" ] ; then
    echo "*** $testindex. ${feature}_disabled: SKIPPED"
  else
    $FEATURETOOL -q disable -f $feature
    runtest ${feature}_disabled || exitcode=1
  fi
  testindex=$((testindex+1))
done

exit $exitcode
